import json
import os

class KnowledgePointManager:
    """知识点管理类"""
    def __init__(self):
        # 从data/knowledge.json文件读取知识点数据
        self.knowledge_points = self.load_knowledge_points()
    
    def load_knowledge_points(self):
        """从文件加载知识点数据"""
        try:
            with open("data/knowledge.json", 'r', encoding='utf-8') as f:
                return json.load(f)
        except Exception as e:
            print(f"加载知识点数据失败: {e}")
            return {}
    
    def get_grades(self):
        """获取所有年级"""
        return list(self.knowledge_points.keys())
    
    def get_subjects(self, grade):
        """获取指定年级的所有科目"""
        if grade in self.knowledge_points:
            return list(self.knowledge_points[grade].keys())
        return []
    
    def get_knowledge_points(self, grade, subject):
        """获取指定年级和科目的所有知识点"""
        if grade in self.knowledge_points and subject in self.knowledge_points[grade]:
            return [(kp["name"], kp["id"]) for kp in self.knowledge_points[grade][subject]]
        return []